import warnings
from django.utils.encoding import force_str
from rest_framework import RemovedInDRF317Warning
from django.utils.translation import gettext_lazy as _
from django.http import HttpResponse

from django.shortcuts import render
import json
from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework import status
from django.conf import settings
import os


# Create your views here.
from .serializers import *
from rest_framework import viewsets
from .models import *


from rest_framework.pagination import PageNumberPagination
from rest_framework.decorators import action

# Create your views here.
from django_filters.rest_framework import DjangoFilterBackend, CharFilter

from django_filters import FilterSet, DateFilter

from django.utils.timezone import make_aware
from datetime import datetime, timedelta

from django.http import HttpResponse
from datetime import datetime

# views.py
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import status
from rest_framework.schemas import AutoSchema, coreapi
import coreschema

# from rest_framework.schemas.openapi import AutoSchema
from rest_framework import serializers
from coreapi import Field
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

from django.db.models import F


# 用于解决数据库中日期字段类型为char时，筛选指定日期范围的数据时，起始日期不包含的问题，所需的模块导入
from django.db.models import Q
from django.db.models.functions import Cast
from django.db.models import Value
from django.utils.timezone import make_aware


def testdb(request):
    test1 = Test(name="runoob", sex="男")
    test1.save()
    return HttpResponse("<p>数据添加成功！</p>")


def getdb(request):
    test1 = Test.objects.filter(id=1)
    print(test1)
    return HttpResponse("<p>数据获取成功成功！</p>")


class my_andonCall(APIView):
    # 设置默认的每页数据量
    page_size = 10
    # 设置在API DOC的字段显示
    schema = AutoSchema(
        manual_fields=[
            # Field(name="id", required=False, location="query", schema=coreschema.Number(description="id")),
            # Field(
            #     name="eun", required=False, location="query", schema=coreschema.String(description="发动机号")
            # ),
            Field(
                name="sub_area",
                required=False,
                location="query",
                schema=coreschema.String(description="区域"),
            ),
            # Field(
            #     name="station_type",
            #     required=False,
            #     location="query",
            #     schema=coreschema.String(description="工位类型(A:自动站;M;手动站)"),
            # ),
            Field(name="page", required=False, location="query", schema=coreschema.String(description="第几页")),
            Field(
                name="page_size",
                required=False,
                location="query",
                schema=coreschema.String(description="页面大小"),
            ),
            Field(
                name="start_time",
                required=False,
                location="query",
                schema=coreschema.String(description="开始日期时间，格式示例：2024-07-01 16:34:42"),
            ),
            Field(
                name="end_time",
                required=False,
                location="query",
                schema=coreschema.String(description="结束日期时间，格式示例：2024-07-10 16:34:42"),
            ),
        ]
    )

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = AndonCallSerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def get_queryset(self, request, *args, **kwargs):
        # 这里实现获取数据集合的逻辑
        # 例如: return Model.objects.all()
        # 创建一个基础的查询集
        # queryset = OutputLoopa.objects.using("as_manage_analysis").annotate(staion_order=F('staion')).order_by('staion_order')
        queryset = HelpCallHistory.objects.using("sql_server_dtrdb").filter().order_by("start_time")
        # 获取请求的参数

        start_time_param = request.query_params.get("start_time", None)
        end_time_param = request.query_params.get("end_time", None)
        print(start_time_param)
        print(end_time_param)

        id = request.query_params.get("id")
        sub_area = request.query_params.get("sub_area")
        # station_type = request.query_params.get("station_type")
        # eun = request.query_params.get("eun")
        # 根据参数是否存在添加不同筛选条件
        if id is not None:
            queryset = HelpCallHistory.objects.using("sql_server_dtrdb").filter(id=id)
        if sub_area is not None:
            queryset = queryset.filter(sub_area__contains=sub_area)
        if (start_time_param and end_time_param) is not None:
            start_time_param_trans = datetime.strptime(start_time_param, "%Y-%m-%d %H:%M:%S")
            end_time_param_trans = datetime.strptime(end_time_param, "%Y-%m-%d %H:%M:%S")
            print(start_time_param_trans)
            print(end_time_param_trans)
            queryset = queryset.filter(start_time__gte=start_time_param_trans, start_time__lte=end_time_param_trans)
        else:
            print("aaaaa")

        return queryset


class MachineFaultHistoryView(APIView):
    # 设置默认的每页数据量
    page_size = 10
    # 设置在API DOC的字段显示
    schema = AutoSchema(
        manual_fields=[
            Field(
                name="point_id",
                required=False,
                location="query",
                schema=coreschema.Number(description="point_id"),
            ),
            # Field(
            #     name="eun", required=False, location="query", schema=coreschema.String(description="发动机号")
            # ),
            Field(
                name="sub_area",
                required=False,
                location="query",
                schema=coreschema.String(description="区域"),
            ),
            Field(
                name="equipment_id",
                required=False,
                location="query",
                schema=coreschema.String(description="工位"),
            ),
            Field(name="page", required=False, location="query", schema=coreschema.String(description="第几页")),
            Field(
                name="page_size",
                required=False,
                location="query",
                schema=coreschema.String(description="页面大小"),
            ),
            Field(
                name="start_time",
                required=False,
                location="query",
                schema=coreschema.String(description="开始日期时间，格式示例：2024-07-01 16:34:42"),
            ),
            Field(
                name="end_time",
                required=False,
                location="query",
                schema=coreschema.String(description="结束日期时间，格式示例：2024-07-10 16:34:42"),
            ),
        ]
    )

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = AndonCallSerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def get_queryset(self, request, *args, **kwargs):
        # 这里实现获取数据集合的逻辑
        # 例如: return Model.objects.all()
        # 创建一个基础的查询集
        # queryset = OutputLoopa.objects.using("as_manage_analysis").annotate(staion_order=F('staion')).order_by('staion_order')
        queryset = FltHistory.objects.using("sql_server_dtrdb").filter().order_by("start_time")
        # 获取请求的参数

        start_time_param = request.query_params.get("start_time", None)
        end_time_param = request.query_params.get("end_time", None)

        point_id = request.query_params.get("point_id")
        sub_area = request.query_params.get("sub_area")
        equipment_id = request.query_params.get("equipment_id")
        # 根据参数是否存在添加不同筛选条件
        if point_id is not None:
            queryset = queryset.filter(point_id=point_id)
        if sub_area is not None:
            queryset = queryset.filter(sub_area__contains=sub_area)
        if equipment_id is not None:
            queryset = queryset.filter(equipment_id__contains=equipment_id)
        if (start_time_param and end_time_param) is not None:
            start_time_param_trans = datetime.strptime(start_time_param, "%Y-%m-%d %H:%M:%S")
            end_time_param_trans = datetime.strptime(end_time_param, "%Y-%m-%d %H:%M:%S")
            print(start_time_param_trans)
            print(end_time_param_trans)
            queryset = queryset.filter(start_time__gte=start_time_param_trans, start_time__lte=end_time_param_trans)
        else:
            print("aaaaa")

        return queryset


class StationStatusView(APIView):
    # 设置默认的每页数据量
    page_size = 10
    # 设置在API DOC的字段显示
    schema = AutoSchema(
        manual_fields=[
            Field(name="id", required=False, location="query", schema=coreschema.Number(description="id")),
            Field(
                name="point_id",
                required=False,
                location="query",
                schema=coreschema.String(description="point_id"),
            ),
            Field(
                name="sub_area",
                required=False,
                location="query",
                schema=coreschema.String(description="区域"),
            ),
            Field(
                name="equipment_id",
                required=False,
                location="query",
                schema=coreschema.String(description="工位"),
            ),
            Field(name="page", required=False, location="query", schema=coreschema.String(description="第几页")),
            Field(
                name="page_size",
                required=False,
                location="query",
                schema=coreschema.String(description="页面大小"),
            ),
            Field(
                name="start_time",
                required=False,
                location="query",
                schema=coreschema.String(description="开始日期时间，格式示例：2024-07-01 16:34:42"),
            ),
            Field(
                name="end_time",
                required=False,
                location="query",
                schema=coreschema.String(description="结束日期时间，格式示例：2024-07-10 16:34:42"),
            ),
        ]
    )

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = StationStatusSerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def get_queryset(self, request, *args, **kwargs):
        # 这里实现获取数据集合的逻辑
        # 例如: return Model.objects.all()
        # 创建一个基础的查询集
        # queryset = OutputLoopa.objects.using("as_manage_analysis").annotate(staion_order=F('staion')).order_by('staion_order')
        queryset = StrvBlkHistory.objects.using("sql_server_dtrdb").filter().order_by("start_time")
        # 获取请求的参数

        start_time_param = request.query_params.get("start_time", None)
        end_time_param = request.query_params.get("end_time", None)

        id = request.query_params.get("id")
        sub_area = request.query_params.get("sub_area")
        equipment_id = request.query_params.get("equipment_id")
        # 根据参数是否存在添加不同筛选条件
        if id is not None:
            queryset = StrvBlkHistory.objects.using("sql_server_dtrdb").filter(id=id)
        if sub_area is not None:
            queryset = queryset.filter(sub_area__contains=sub_area)
        if equipment_id is not None:
            queryset = queryset.filter(equipment_id__contains=equipment_id)
        if (start_time_param and end_time_param) is not None:
            start_time_param_trans = datetime.strptime(start_time_param, "%Y-%m-%d %H:%M:%S")
            end_time_param_trans = datetime.strptime(end_time_param, "%Y-%m-%d %H:%M:%S")
            print(start_time_param_trans)
            print(end_time_param_trans)
            queryset = queryset.filter(start_time__gte=start_time_param_trans, start_time__lte=end_time_param_trans)
        else:
            print("aaaaa")

        return queryset


class EquipmentView(APIView):
    # 设置默认的每页数据量
    page_size = 10
    # 设置在API DOC的字段显示
    schema = AutoSchema(
        manual_fields=[
            Field(name="id", required=False, location="query", schema=coreschema.Number(description="id")),
            Field(
                name="point_id",
                required=False,
                location="query",
                schema=coreschema.String(description="point_id"),
            ),
            Field(
                name="sub_area",
                required=False,
                location="query",
                schema=coreschema.String(description="区域"),
            ),
            Field(
                name="line",
                required=False,
                location="query",
                schema=coreschema.String(description="线体"),
            ),
            Field(
                name="equipment_id",
                required=False,
                location="query",
                schema=coreschema.String(description="工位"),
            ),
            Field(name="page", required=False, location="query", schema=coreschema.String(description="第几页")),
            Field(
                name="page_size",
                required=False,
                location="query",
                schema=coreschema.String(description="页面大小"),
            ),
            Field(
                name="start_time",
                required=False,
                location="query",
                schema=coreschema.String(description="开始日期时间，格式示例：2024-07-01 16:34:42"),
            ),
            Field(
                name="end_time",
                required=False,
                location="query",
                schema=coreschema.String(description="结束日期时间，格式示例：2024-07-10 16:34:42"),
            ),
        ]
    )

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = EquipmentSerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def get_queryset(self, request, *args, **kwargs):
        # 这里实现获取数据集合的逻辑
        # 例如: return Model.objects.all()
        # 创建一个基础的查询集
        # queryset = OutputLoopa.objects.using("as_manage_analysis").annotate(staion_order=F('staion')).order_by('staion_order')
        queryset = PointIdGd.objects.using("sql_server_dtrdb").filter().order_by("id")
        # 获取请求的参数

        start_time_param = request.query_params.get("start_time", None)
        end_time_param = request.query_params.get("end_time", None)

        id = request.query_params.get("id")
        sub_area = request.query_params.get("sub_area")
        equipment_id = request.query_params.get("equipment_id")
        区域 = request.query_params.get("line")
        # 根据参数是否存在添加不同筛选条件
        if id is not None:
            queryset = queryset.filter(id=id)
        if sub_area is not None:
            queryset = queryset.filter(sub_area__contains=sub_area)
        if 区域 is not None:
            queryset = queryset.filter(区域__contains=区域)
        if equipment_id is not None:
            queryset = queryset.filter(equipment_id__contains=equipment_id)
        if (start_time_param and end_time_param) is not None:
            start_time_param_trans = datetime.strptime(start_time_param, "%Y-%m-%d %H:%M:%S")
            end_time_param_trans = datetime.strptime(end_time_param, "%Y-%m-%d %H:%M:%S")
            print(start_time_param_trans)
            print(end_time_param_trans)
            queryset = queryset.filter(start_time__gte=start_time_param_trans, start_time__lte=end_time_param_trans)
        else:
            print("aaaaa")

        return queryset


class LastCtStatisticsView(APIView):
    # 设置默认的每页数据量
    page_size = 10
    # 设置在API DOC的字段显示
    schema = AutoSchema(
        manual_fields=[
            Field(name="id", required=False, location="query", schema=coreschema.Number(description="id")),
            Field(
                name="point_id",
                required=False,
                location="query",
                schema=coreschema.String(description="point_id"),
            ),
            Field(
                name="shift",
                required=False,
                location="query",
                schema=coreschema.Number(description="shift"),
            ),
            Field(
                name="hour",
                required=False,
                location="query",
                schema=coreschema.Number(description="小时"),
            ),
            Field(
                name="sub_area",
                required=False,
                location="query",
                schema=coreschema.String(description="区域"),
            ),
            Field(
                name="equipment_id",
                required=False,
                location="query",
                schema=coreschema.String(description="工位"),
            ),
            Field(name="page", required=False, location="query", schema=coreschema.String(description="第几页")),
            Field(
                name="page_size",
                required=False,
                location="query",
                schema=coreschema.String(description="页面大小"),
            ),
            Field(
                name="start_time",
                required=False,
                location="query",
                schema=coreschema.String(description="开始日期时间，格式示例：2024-07-01 16:34:42"),
            ),
            Field(
                name="end_time",
                required=False,
                location="query",
                schema=coreschema.String(description="结束日期时间，格式示例：2024-07-10 16:34:42"),
            ),
        ]
    )

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = LastCtStatisticsSerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def get_queryset(self, request, *args, **kwargs):
        # 这里实现获取数据集合的逻辑
        # 例如: return Model.objects.all()
        # 创建一个基础的查询集
        # queryset = OutputLoopa.objects.using("as_manage_analysis").annotate(staion_order=F('staion')).order_by('staion_order')
        queryset = LastCtStatistics.objects.using("sql_server_dtrdb").filter().order_by("id")
        # 获取请求的参数

        start_time_param = request.query_params.get("start_time", None)
        end_time_param = request.query_params.get("end_time", None)

        id = request.query_params.get("id")
        sub_area = request.query_params.get("sub_area")
        equipment_id = request.query_params.get("equipment_id")
        shift = request.query_params.get("shift")
        hour = request.query_params.get("hour")
        # 根据参数是否存在添加不同筛选条件
        if id is not None:
            queryset = queryset.filter(id=id)
        if sub_area is not None:
            queryset = queryset.filter(sub_area__contains=sub_area)
        if shift is not None:
            queryset = queryset.filter(shift__contains=shift)
        if hour is not None:
            queryset = queryset.filter(hour__contains=hour)
        if equipment_id is not None:
            queryset = queryset.filter(equipment_id__contains=equipment_id)
        if (start_time_param and end_time_param) is not None:
            start_time_param_trans = datetime.strptime(start_time_param, "%Y-%m-%d %H:%M:%S")
            end_time_param_trans = datetime.strptime(end_time_param, "%Y-%m-%d %H:%M:%S")
            print(start_time_param_trans)
            print(end_time_param_trans)
            # queryset = queryset.filter(start_time__gte=start_time_param_trans, start_time__lte=end_time_param_trans)
            # 设定想要筛选的日期范围
            start_date = make_aware(datetime(start_time_param_trans.year, start_time_param_trans.month, start_time_param_trans.day))
            end_date = make_aware(datetime(end_time_param_trans.year, end_time_param_trans.month, end_time_param_trans.day))
            # 使用Cast将字符串转换为日期类型，然后进行比较
            queryset = queryset.annotate(production_date_as_date=Cast("production_date", models.DateField())).filter(Q(production_date_as_date__gte=start_date) & Q(production_date_as_date__lte=end_date))

        else:
            print("aaaaa")

        return queryset


class ToolChangeLogView(APIView):
    # 设置默认的每页数据量
    page_size = 10
    # 设置在API DOC的字段显示
    schema = AutoSchema(
        manual_fields=[
            Field(name="id", required=False, location="query", schema=coreschema.Number(description="id")),
            Field(
                name="equipment",
                required=False,
                location="query",
                schema=coreschema.String(description="equipment"),
            ),
            Field(
                name="sub_area",
                required=False,
                location="query",
                schema=coreschema.String(description="sub_area"),
            ),
            Field(
                name="shift",
                required=False,
                location="query",
                schema=coreschema.Number(description="shift"),
            ),
            Field(
                name="project",
                required=False,
                location="query",
                schema=coreschema.String(description="project"),
            ),
            Field(name="page", required=False, location="query", schema=coreschema.String(description="第几页")),
            Field(
                name="page_size",
                required=False,
                location="query",
                schema=coreschema.String(description="页面大小"),
            ),
            Field(
                name="start_time",
                required=False,
                location="query",
                schema=coreschema.String(description="开始日期时间，格式示例：2024-07-01 16:34:42"),
            ),
            Field(
                name="end_time",
                required=False,
                location="query",
                schema=coreschema.String(description="结束日期时间，格式示例：2024-07-10 16:34:42"),
            ),
        ]
    )

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = ToolChangeLogSerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def get_queryset(self, request, *args, **kwargs):
        # 这里实现获取数据集合的逻辑
        # 例如: return Model.objects.all()
        # 创建一个基础的查询集
        # queryset = OutputLoopa.objects.using("as_manage_analysis").annotate(staion_order=F('staion')).order_by('staion_order')
        queryset = ToolChangeLog.objects.using("sql_server_dtrdb").filter().order_by("id")
        # 获取请求的参数

        start_time_param = request.query_params.get("start_time", None)
        end_time_param = request.query_params.get("end_time", None)

        id = request.query_params.get("id")
        project = request.query_params.get("project")
        equipment = request.query_params.get("equipment")
        sub_area = request.query_params.get("sub_area")
        shift = request.query_params.get("shift")
        call_type = request.query_params.get("call_type")
        # 根据参数是否存在添加不同筛选条件
        if id is not None:
            queryset = queryset.filter(id=id)
        if project is not None:
            queryset = queryset.filter(project__contains=project)
        if shift is not None:
            queryset = queryset.filter(shift__contains=shift)
        if equipment is not None:
            queryset = queryset.filter(equipment__contains=equipment)
        if sub_area is not None:
            queryset = queryset.filter(sub_area__contains=sub_area)
        if (start_time_param and end_time_param) is not None:
            start_time_param_trans = datetime.strptime(start_time_param, "%Y-%m-%d %H:%M:%S")
            end_time_param_trans = datetime.strptime(end_time_param, "%Y-%m-%d %H:%M:%S")
            print(start_time_param_trans)
            print(end_time_param_trans)
            queryset = queryset.filter(timestamp__gte=start_time_param_trans, timestamp__lte=end_time_param_trans)
        else:
            print("aaaaa")

        return queryset


class ScheduleView(APIView):
    # 设置默认的每页数据量
    page_size = 10
    # 设置在API DOC的字段显示
    schema = AutoSchema(
        manual_fields=[
            Field(name="id", required=False, location="query", schema=coreschema.Number(description="id")),
            Field(
                name="sub_area",
                required=False,
                location="query",
                schema=coreschema.String(description="sub_area"),
            ),
            Field(
                name="shift",
                required=False,
                location="query",
                schema=coreschema.Number(description="shift"),
            ),
            Field(
                name="production_date",
                required=False,
                location="query",
                schema=coreschema.String(description="production_date生产日期"),
            ),
            Field(name="page", required=False, location="query", schema=coreschema.String(description="第几页")),
            Field(
                name="page_size",
                required=False,
                location="query",
                schema=coreschema.String(description="页面大小"),
            ),
            Field(
                name="start_time",
                required=False,
                location="query",
                schema=coreschema.String(description="开始日期时间，格式示例：2024-07-01 16:34:42"),
            ),
            Field(
                name="end_time",
                required=False,
                location="query",
                schema=coreschema.String(description="结束日期时间，格式示例：2024-07-10 16:34:42"),
            ),
        ]
    )

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = ScheduleSerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def get_queryset(self, request, *args, **kwargs):
        # 这里实现获取数据集合的逻辑
        # 例如: return Model.objects.all()
        # 创建一个基础的查询集
        # queryset = OutputLoopa.objects.using("as_manage_analysis").annotate(staion_order=F('staion')).order_by('staion_order')
        queryset = Schedule.objects.using("sql_server_dtrdb").filter().order_by("id")
        # 获取请求的参数

        start_time_param = request.query_params.get("start_time", None)
        end_time_param = request.query_params.get("end_time", None)

        id = request.query_params.get("id")
        production_date = request.query_params.get("production_date")
        # equipment = request.query_params.get("equipment")
        sub_area = request.query_params.get("sub_area")
        shift = request.query_params.get("shift")
        # call_type = request.query_params.get("call_type")
        # 根据参数是否存在添加不同筛选条件
        if id is not None:
            queryset = queryset.filter(id=id)
        if production_date is not None:
            queryset = queryset.filter(production_date__contains=production_date)
        if shift is not None:
            queryset = queryset.filter(shift__contains=shift)
        # if equipment is not None:
        #     queryset = queryset.filter(equipment_id__contains=equipment)
        if sub_area is not None:
            queryset = queryset.filter(sub_area__contains=sub_area)
        if (start_time_param and end_time_param) is not None:
            start_time_param_trans = datetime.strptime(start_time_param, "%Y-%m-%d %H:%M:%S")
            end_time_param_trans = datetime.strptime(end_time_param, "%Y-%m-%d %H:%M:%S")
            print(start_time_param_trans)
            print(end_time_param_trans)
            queryset = queryset.filter(shift_start_time__gte=start_time_param_trans, shift_start_time__lte=end_time_param_trans)
        else:
            print("aaaaa")

        return queryset


class ProductionHourlyView(APIView):
    # 设置默认的每页数据量
    page_size = 10
    # 设置在API DOC的字段显示
    schema = AutoSchema(
        manual_fields=[
            Field(name="id", required=False, location="query", schema=coreschema.Number(description="id")),
            Field(
                name="point_id",
                required=False,
                location="query",
                schema=coreschema.String(description="point_id"),
            ),
            Field(
                name="shift",
                required=False,
                location="query",
                schema=coreschema.Number(description="shift"),
            ),
            Field(name="page", required=False, location="query", schema=coreschema.String(description="第几页")),
            Field(
                name="page_size",
                required=False,
                location="query",
                schema=coreschema.String(description="页面大小"),
            ),
            Field(
                name="start_time",
                required=False,
                location="query",
                schema=coreschema.String(description="开始日期时间，格式示例：2024-07-01"),
            ),
            Field(
                name="end_time",
                required=False,
                location="query",
                schema=coreschema.String(description="结束日期时间，格式示例：2024-07-10"),
            ),
        ]
    )

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = ProductionHourlySerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def get_queryset(self, request, *args, **kwargs):
        # 这里实现获取数据集合的逻辑
        # 例如: return Model.objects.all()
        # 创建一个基础的查询集
        # queryset = OutputLoopa.objects.using("as_manage_analysis").annotate(staion_order=F('staion')).order_by('staion_order')
        queryset = GdNow.objects.using("sql_server_dtrdb").filter().order_by("id")
        # 获取请求的参数

        start_time_param = request.query_params.get("start_time", None)
        end_time_param = request.query_params.get("end_time", None)

        id = request.query_params.get("id")
        point_id = request.query_params.get("point_id")
        shift = request.query_params.get("shift")
        # 根据参数是否存在添加不同筛选条件
        if id is not None:
            queryset = queryset.filter(id=id)
        if point_id is not None:
            queryset = queryset.filter(point_id__contains=point_id)
        if shift is not None:
            queryset = queryset.filter(shift__contains=shift)
        if (start_time_param and end_time_param) is not None:
            start_time_param_trans = datetime.strptime(start_time_param, "%Y-%m-%d")
            end_time_param_trans = datetime.strptime(end_time_param, "%Y-%m-%d")
            print(start_time_param_trans)
            print(end_time_param_trans)
            # queryset = queryset.filter(production_date__gte=start_time_param_trans, production_date__lte=end_time_param_trans)
            # 设定想要筛选的日期范围
            start_date = make_aware(datetime(start_time_param_trans.year, start_time_param_trans.month, start_time_param_trans.day))
            end_date = make_aware(datetime(end_time_param_trans.year, end_time_param_trans.month, end_time_param_trans.day))
            # 使用Cast将字符串转换为日期类型，然后进行比较
            queryset = queryset.annotate(production_date_as_date=Cast("production_date", models.DateField())).filter(Q(production_date_as_date__gte=start_date) & Q(production_date_as_date__lte=end_date))

        else:
            print("aaaaa")

        return queryset


class LastCtView(APIView):
    # 设置默认的每页数据量
    page_size = 10
    # 设置在API DOC的字段显示
    schema = AutoSchema(
        manual_fields=[
            Field(name="id", required=False, location="query", schema=coreschema.Number(description="id")),
            Field(
                name="point_id",
                required=False,
                location="query",
                schema=coreschema.String(description="point_id"),
            ),
            Field(name="page", required=False, location="query", schema=coreschema.String(description="第几页")),
            Field(
                name="page_size",
                required=False,
                location="query",
                schema=coreschema.String(description="页面大小"),
            ),
            Field(
                name="start_time",
                required=False,
                location="query",
                schema=coreschema.String(description="开始日期时间，格式示例：2024-07-01 16:34:42"),
            ),
            Field(
                name="end_time",
                required=False,
                location="query",
                schema=coreschema.String(description="结束日期时间，格式示例：2024-07-10 16:34:42"),
            ),
        ]
    )

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = LastCtSerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def get_queryset(self, request, *args, **kwargs):
        # 这里实现获取数据集合的逻辑
        # 例如: return Model.objects.all()
        # 创建一个基础的查询集
        # queryset = OutputLoopa.objects.using("as_manage_analysis").annotate(staion_order=F('staion')).order_by('staion_order')
        queryset = LastCt.objects.using("sql_server_dtrdb").filter().order_by("id")
        # 获取请求的参数

        start_time_param = request.query_params.get("start_time", None)
        end_time_param = request.query_params.get("end_time", None)

        id = request.query_params.get("id")
        point_id = request.query_params.get("point_id")
        # 根据参数是否存在添加不同筛选条件
        if id is not None:
            queryset = queryset.filter(id=id)
        if point_id is not None:
            queryset = queryset.filter(point_id__contains=point_id)
        if (start_time_param and end_time_param) is not None:
            start_time_param_trans = datetime.strptime(start_time_param, "%Y-%m-%d %H:%M:%S")
            end_time_param_trans = datetime.strptime(end_time_param, "%Y-%m-%d %H:%M:%S")
            print(start_time_param_trans)
            print(end_time_param_trans)
            queryset = queryset.filter(timestamp__gte=start_time_param_trans, timestamp__lte=end_time_param_trans)
        else:
            print("aaaaa")

        return queryset


class AS_LastCt_filterView(APIView):
    # 设置默认的每页数据量
    page_size = 10
    # 设置在API DOC的字段显示
    schema = AutoSchema(
        manual_fields=[
            Field(name="id", required=False, location="query", schema=coreschema.Number(description="id")),
            Field(
                name="point_id",
                required=False,
                location="query",
                schema=coreschema.String(description="point_id"),
            ),
            Field(name="page", required=False, location="query", schema=coreschema.String(description="第几页")),
            Field(
                name="page_size",
                required=False,
                location="query",
                schema=coreschema.String(description="页面大小"),
            ),
            Field(
                name="start_time",
                required=False,
                location="query",
                schema=coreschema.String(description="开始日期时间，格式示例：2024-07-01 16:34:42"),
            ),
            Field(
                name="end_time",
                required=False,
                location="query",
                schema=coreschema.String(description="结束日期时间，格式示例：2024-07-10 16:34:42"),
            ),
        ]
    )

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = AS_LastCt_filterSerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def get_queryset(self, request, *args, **kwargs):
        # 这里实现获取数据集合的逻辑
        # 例如: return Model.objects.all()
        # 创建一个基础的查询集
        # queryset = OutputLoopa.objects.using("as_manage_analysis").annotate(staion_order=F('staion')).order_by('staion_order')
        queryset = ValOvertwoOoandlessoneOoo.objects.using("sql_server_dtrdb").filter().order_by("id")
        # 获取请求的参数

        start_time_param = request.query_params.get("start_time", None)
        end_time_param = request.query_params.get("end_time", None)

        id = request.query_params.get("id")
        point_id = request.query_params.get("point_id")
        # 根据参数是否存在添加不同筛选条件
        if id is not None:
            queryset = queryset.filter(id=id)
        if point_id is not None:
            queryset = queryset.filter(point_id__contains=point_id)
        if (start_time_param and end_time_param) is not None:
            start_time_param_trans = datetime.strptime(start_time_param, "%Y-%m-%d %H:%M:%S")
            end_time_param_trans = datetime.strptime(end_time_param, "%Y-%m-%d %H:%M:%S")
            print(start_time_param_trans)
            print(end_time_param_trans)
            queryset = queryset.filter(timestamp__gte=start_time_param_trans, timestamp__lte=end_time_param_trans)
        else:
            print("aaaaa")

        return queryset


class MS_LastCt_filterView(APIView):
    # 设置默认的每页数据量
    page_size = 10
    # 设置在API DOC的字段显示
    schema = AutoSchema(
        manual_fields=[
            Field(name="id", required=False, location="query", schema=coreschema.Number(description="id")),
            Field(
                name="point_id",
                required=False,
                location="query",
                schema=coreschema.String(description="point_id"),
            ),
            Field(name="page", required=False, location="query", schema=coreschema.String(description="第几页")),
            Field(
                name="page_size",
                required=False,
                location="query",
                schema=coreschema.String(description="页面大小"),
            ),
            Field(
                name="start_time",
                required=False,
                location="query",
                schema=coreschema.String(description="开始日期时间，格式示例：2024-07-01 16:34:42"),
            ),
            Field(
                name="end_time",
                required=False,
                location="query",
                schema=coreschema.String(description="结束日期时间，格式示例：2024-07-10 16:34:42"),
            ),
        ]
    )

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = MS_LastCt_filterSerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def get_queryset(self, request, *args, **kwargs):
        # 这里实现获取数据集合的逻辑
        # 例如: return Model.objects.all()
        # 创建一个基础的查询集
        # queryset = OutputLoopa.objects.using("as_manage_analysis").annotate(staion_order=F('staion')).order_by('staion_order')
        queryset = MsLastCtFilter.objects.using("sql_server_dtrdb").filter().order_by("id")
        # 获取请求的参数

        start_time_param = request.query_params.get("start_time", None)
        end_time_param = request.query_params.get("end_time", None)

        id = request.query_params.get("id")
        point_id = request.query_params.get("point_id")
        # 根据参数是否存在添加不同筛选条件
        if id is not None:
            queryset = queryset.filter(id=id)
        if point_id is not None:
            queryset = queryset.filter(point_id__contains=point_id)
        if (start_time_param and end_time_param) is not None:
            start_time_param_trans = datetime.strptime(start_time_param, "%Y-%m-%d %H:%M:%S")
            end_time_param_trans = datetime.strptime(end_time_param, "%Y-%m-%d %H:%M:%S")
            print(start_time_param_trans)
            print(end_time_param_trans)
            queryset = queryset.filter(timestamp__gte=start_time_param_trans, timestamp__lte=end_time_param_trans)
        else:
            print("aaaaa")

        return queryset


class LoopA_hourly_productionView(APIView):
    # 设置默认的每页数据量
    page_size = 10
    # 设置在API DOC的字段显示
    schema = AutoSchema(
        manual_fields=[
            Field(name="id", required=False, location="query", schema=coreschema.Number(description="id")),
            Field(name="page", required=False, location="query", schema=coreschema.String(description="第几页")),
            Field(
                name="page_size",
                required=False,
                location="query",
                schema=coreschema.String(description="页面大小"),
            ),
            Field(
                name="start_time",
                required=False,
                location="query",
                schema=coreschema.String(description="开始日期时间，格式示例：2024-07-01 16:34:42"),
            ),
            Field(
                name="end_time",
                required=False,
                location="query",
                schema=coreschema.String(description="结束日期时间，格式示例：2024-07-10 16:34:42"),
            ),
        ]
    )

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = LoopA_hourly_productionSerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def get_queryset(self, request, *args, **kwargs):
        # 这里实现获取数据集合的逻辑
        # 例如: return Model.objects.all()
        # 创建一个基础的查询集
        # queryset = OutputLoopa.objects.using("as_manage_analysis").annotate(staion_order=F('staion')).order_by('staion_order')
        queryset = GdAs01.objects.using("sql_server_dtrdb").filter().order_by("timestamp")
        # 获取请求的参数

        start_time_param = request.query_params.get("start_time", None)
        end_time_param = request.query_params.get("end_time", None)

        id = request.query_params.get("id")
        # 根据参数是否存在添加不同筛选条件
        if id is not None:
            queryset = queryset.filter(id=id)
        if (start_time_param and end_time_param) is not None:
            start_time_param_trans = datetime.strptime(start_time_param, "%Y-%m-%d %H:%M:%S")
            end_time_param_trans = datetime.strptime(end_time_param, "%Y-%m-%d %H:%M:%S")
            print(start_time_param_trans)
            print(end_time_param_trans)
            queryset = queryset.filter(timestamp__gte=start_time_param_trans, timestamp__lte=end_time_param_trans)
        else:
            print("aaaaa")

        return queryset


class LoopB_hourly_productionView(APIView):
    # 设置默认的每页数据量
    page_size = 10
    # 设置在API DOC的字段显示
    schema = AutoSchema(
        manual_fields=[
            Field(name="id", required=False, location="query", schema=coreschema.Number(description="id")),
            Field(name="page", required=False, location="query", schema=coreschema.String(description="第几页")),
            Field(
                name="page_size",
                required=False,
                location="query",
                schema=coreschema.String(description="页面大小"),
            ),
            Field(
                name="start_time",
                required=False,
                location="query",
                schema=coreschema.String(description="开始日期时间，格式示例：2024-07-01 16:34:42"),
            ),
            Field(
                name="end_time",
                required=False,
                location="query",
                schema=coreschema.String(description="结束日期时间，格式示例：2024-07-10 16:34:42"),
            ),
        ]
    )

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = LoopB_hourly_productionSerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def get_queryset(self, request, *args, **kwargs):
        # 这里实现获取数据集合的逻辑
        # 例如: return Model.objects.all()
        # 创建一个基础的查询集
        # queryset = OutputLoopa.objects.using("as_manage_analysis").annotate(staion_order=F('staion')).order_by('staion_order')
        queryset = GdAh01.objects.using("sql_server_dtrdb").filter().order_by("timestamp")
        # 获取请求的参数

        start_time_param = request.query_params.get("start_time", None)
        end_time_param = request.query_params.get("end_time", None)

        id = request.query_params.get("id")
        # 根据参数是否存在添加不同筛选条件
        if id is not None:
            queryset = queryset.filter(id=id)
        if (start_time_param and end_time_param) is not None:
            start_time_param_trans = datetime.strptime(start_time_param, "%Y-%m-%d %H:%M:%S")
            end_time_param_trans = datetime.strptime(end_time_param, "%Y-%m-%d %H:%M:%S")
            print(start_time_param_trans)
            print(end_time_param_trans)
            queryset = queryset.filter(timestamp__gte=start_time_param_trans, timestamp__lte=end_time_param_trans)
        else:
            print("aaaaa")

        return queryset


class LoopC_hourly_productionView(APIView):
    # 设置默认的每页数据量
    page_size = 10
    # 设置在API DOC的字段显示
    schema = AutoSchema(
        manual_fields=[
            Field(name="id", required=False, location="query", schema=coreschema.Number(description="id")),
            Field(name="page", required=False, location="query", schema=coreschema.String(description="第几页")),
            Field(
                name="page_size",
                required=False,
                location="query",
                schema=coreschema.String(description="页面大小"),
            ),
            Field(
                name="start_time",
                required=False,
                location="query",
                schema=coreschema.String(description="开始日期时间，格式示例：2024-07-01 16:34:42"),
            ),
            Field(
                name="end_time",
                required=False,
                location="query",
                schema=coreschema.String(description="结束日期时间，格式示例：2024-07-10 16:34:42"),
            ),
        ]
    )

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = LoopC_hourly_productionSerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def get_queryset(self, request, *args, **kwargs):
        # 这里实现获取数据集合的逻辑
        # 例如: return Model.objects.all()
        # 创建一个基础的查询集
        # queryset = OutputLoopa.objects.using("as_manage_analysis").annotate(staion_order=F('staion')).order_by('staion_order')
        queryset = GdAc01.objects.using("sql_server_dtrdb").filter().order_by("timestamp")
        # 获取请求的参数

        start_time_param = request.query_params.get("start_time", None)
        end_time_param = request.query_params.get("end_time", None)

        id = request.query_params.get("id")
        # 根据参数是否存在添加不同筛选条件
        if id is not None:
            queryset = queryset.filter(id=id)
        if (start_time_param and end_time_param) is not None:
            start_time_param_trans = datetime.strptime(start_time_param, "%Y-%m-%d %H:%M:%S")
            end_time_param_trans = datetime.strptime(end_time_param, "%Y-%m-%d %H:%M:%S")
            print(start_time_param_trans)
            print(end_time_param_trans)
            queryset = queryset.filter(timestamp__gte=start_time_param_trans, timestamp__lte=end_time_param_trans)
        else:
            print("aaaaa")

        return queryset


class GdHistoryView(APIView):
    # 设置默认的每页数据量
    page_size = 10
    # 设置在API DOC的字段显示
    schema = AutoSchema(
        manual_fields=[
            Field(name="id", required=False, location="query", schema=coreschema.Number(description="id")),
            Field(name="equipment_id", required=False, location="query", schema=coreschema.String(description="工位号equipment_id")),
            Field(name="sub_area", required=False, location="query", schema=coreschema.String(description="区域sub_area")),
            Field(name="page", required=False, location="query", schema=coreschema.String(description="第几页")),
            Field(
                name="page_size",
                required=False,
                location="query",
                schema=coreschema.String(description="页面大小"),
            ),
            Field(
                name="start_time",
                required=False,
                location="query",
                schema=coreschema.String(description="开始日期时间，格式示例：2024-07-01"),
            ),
            Field(
                name="end_time",
                required=False,
                location="query",
                schema=coreschema.String(description="结束日期时间，格式示例：2024-07-10"),
            ),
        ]
    )

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = GdHistorySerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def get_queryset(self, request, *args, **kwargs):
        # 这里实现获取数据集合的逻辑
        # 例如: return Model.objects.all()
        # 创建一个基础的查询集
        # queryset = OutputLoopa.objects.using("as_manage_analysis").annotate(staion_order=F('staion')).order_by('staion_order')
        queryset = GdHistory.objects.using("sql_server_dtrdb").filter().order_by("id")
        # 获取请求的参数

        start_time_param = request.query_params.get("start_time", None)
        end_time_param = request.query_params.get("end_time", None)

        id = request.query_params.get("id")
        equipment_id = request.query_params.get("equipment_id")
        sub_area = request.query_params.get("sub_area")
        # 根据参数是否存在添加不同筛选条件
        if id is not None:
            queryset = queryset.filter(id=id)
        if equipment_id is not None:
            queryset = queryset.filter(equipment_id__contains=equipment_id)
        if sub_area is not None:
            queryset = queryset.filter(sub_area__contains=sub_area)
        if (start_time_param and end_time_param) is not None:
            start_time_param_trans = datetime.strptime(start_time_param, "%Y-%m-%d")
            end_time_param_trans = datetime.strptime(end_time_param, "%Y-%m-%d")
            print(start_time_param_trans)
            print(type(start_time_param_trans))
            print(end_time_param_trans)

            # queryset = queryset.filter(production_date__gte=start_time_param_trans, production_date__lte=end_time_param_trans)
            # 设定想要筛选的日期范围
            start_date = make_aware(datetime(start_time_param_trans.year, start_time_param_trans.month, start_time_param_trans.day))
            end_date = make_aware(datetime(end_time_param_trans.year, end_time_param_trans.month, end_time_param_trans.day))
            # 使用Cast将字符串转换为日期类型，然后进行比较
            queryset = queryset.annotate(production_date_as_date=Cast("production_date", models.DateField())).filter(Q(production_date_as_date__gte=start_date) & Q(production_date_as_date__lte=end_date))
        else:
            print("aaaaa")

        return queryset


class ScheduleDetailView(APIView):
    # 设置默认的每页数据量
    page_size = 10
    # 设置在API DOC的字段显示
    schema = AutoSchema(
        manual_fields=[
            # Field(name="id", required=False, location="query", schema=coreschema.Number(description="id")),
            Field(
                name="sub_area",
                required=False,
                location="query",
                schema=coreschema.String(description="sub_area"),
            ),
            Field(
                name="shift",
                required=False,
                location="query",
                schema=coreschema.Number(description="shift"),
            ),
            Field(
                name="production_date",
                required=False,
                location="query",
                schema=coreschema.String(description="production_date生产日期"),
            ),
            Field(name="page", required=False, location="query", schema=coreschema.String(description="第几页")),
            Field(
                name="page_size",
                required=False,
                location="query",
                schema=coreschema.String(description="页面大小"),
            ),
            Field(
                name="start_time",
                required=False,
                location="query",
                schema=coreschema.String(description="开始日期时间，格式示例：2024-07-01 16:34:42"),
            ),
            Field(
                name="end_time",
                required=False,
                location="query",
                schema=coreschema.String(description="结束日期时间，格式示例：2024-07-10 16:34:42"),
            ),
        ]
    )

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = ScheduleDetailSerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def get_queryset(self, request, *args, **kwargs):
        # 这里实现获取数据集合的逻辑
        # 例如: return Model.objects.all()
        # 创建一个基础的查询集
        # queryset = OutputLoopa.objects.using("as_manage_analysis").annotate(staion_order=F('staion')).order_by('staion_order')
        queryset = ScheduleDetail.objects.using("sql_server_dtrdb").filter().order_by("production_date", "hour_start_time")
        # 获取请求的参数

        start_time_param = request.query_params.get("start_time", None)
        end_time_param = request.query_params.get("end_time", None)

        # id = request.query_params.get("id")
        production_date = request.query_params.get("production_date")
        # equipment = request.query_params.get("equipment")
        sub_area = request.query_params.get("sub_area")
        shift = request.query_params.get("shift")
        # call_type = request.query_params.get("call_type")
        # 根据参数是否存在添加不同筛选条件
        # if id is not None:
        #     queryset = queryset.filter(id=id)
        if production_date is not None:
            queryset = queryset.filter(production_date__contains=production_date)
        if shift is not None:
            queryset = queryset.filter(shift__contains=shift)
        # if equipment is not None:
        #     queryset = queryset.filter(equipment_id__contains=equipment)
        if sub_area is not None:
            queryset = queryset.filter(sub_area__contains=sub_area)
        if (start_time_param and end_time_param) is not None:
            start_time_param_trans = datetime.strptime(start_time_param, "%Y-%m-%d %H:%M:%S")
            end_time_param_trans = datetime.strptime(end_time_param, "%Y-%m-%d %H:%M:%S")
            print(start_time_param_trans)
            print(end_time_param_trans)
            queryset = queryset.filter(hour_start_time__gte=start_time_param_trans, hour_start_time__lte=end_time_param_trans)
        else:
            print("aaaaa")

        return queryset


class BK_M1_hourly_productionView(APIView):
    # 设置默认的每页数据量
    page_size = 10
    # 设置在API DOC的字段显示
    schema = AutoSchema(
        manual_fields=[
            Field(name="id", required=False, location="query", schema=coreschema.Number(description="id")),
            Field(name="page", required=False, location="query", schema=coreschema.String(description="第几页")),
            Field(
                name="page_size",
                required=False,
                location="query",
                schema=coreschema.String(description="页面大小"),
            ),
            Field(
                name="start_time",
                required=False,
                location="query",
                schema=coreschema.String(description="开始日期时间，格式示例：2024-07-01 16:34:42"),
            ),
            Field(
                name="end_time",
                required=False,
                location="query",
                schema=coreschema.String(description="结束日期时间，格式示例：2024-07-10 16:34:42"),
            ),
        ]
    )

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = BK_M1_hourly_productionSerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def get_queryset(self, request, *args, **kwargs):
        # 这里实现获取数据集合的逻辑
        # 例如: return Model.objects.all()
        # 创建一个基础的查询集
        # queryset = OutputLoopa.objects.using("as_manage_analysis").annotate(staion_order=F('staion')).order_by('staion_order')
        queryset = GdBk01.objects.using("sql_server_dtrdb").filter().order_by("timestamp")
        # 获取请求的参数

        start_time_param = request.query_params.get("start_time", None)
        end_time_param = request.query_params.get("end_time", None)

        id = request.query_params.get("id")
        # 根据参数是否存在添加不同筛选条件
        if id is not None:
            queryset = queryset.filter(id=id)
        if (start_time_param and end_time_param) is not None:
            start_time_param_trans = datetime.strptime(start_time_param, "%Y-%m-%d %H:%M:%S")
            end_time_param_trans = datetime.strptime(end_time_param, "%Y-%m-%d %H:%M:%S")
            print(start_time_param_trans)
            print(end_time_param_trans)
            queryset = queryset.filter(timestamp__gte=start_time_param_trans, timestamp__lte=end_time_param_trans)
        else:
            print("aaaaa")

        return queryset


class BK_M2_hourly_productionView(APIView):
    # 设置默认的每页数据量
    page_size = 10
    # 设置在API DOC的字段显示
    schema = AutoSchema(
        manual_fields=[
            Field(name="id", required=False, location="query", schema=coreschema.Number(description="id")),
            Field(name="page", required=False, location="query", schema=coreschema.String(description="第几页")),
            Field(
                name="page_size",
                required=False,
                location="query",
                schema=coreschema.String(description="页面大小"),
            ),
            Field(
                name="start_time",
                required=False,
                location="query",
                schema=coreschema.String(description="开始日期时间，格式示例：2024-07-01 16:34:42"),
            ),
            Field(
                name="end_time",
                required=False,
                location="query",
                schema=coreschema.String(description="结束日期时间，格式示例：2024-07-10 16:34:42"),
            ),
        ]
    )

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = BK_M2_hourly_productionSerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def get_queryset(self, request, *args, **kwargs):
        # 这里实现获取数据集合的逻辑
        # 例如: return Model.objects.all()
        # 创建一个基础的查询集
        # queryset = OutputLoopa.objects.using("as_manage_analysis").annotate(staion_order=F('staion')).order_by('staion_order')
        queryset = GdBk02.objects.using("sql_server_dtrdb").filter().order_by("timestamp")
        # 获取请求的参数

        start_time_param = request.query_params.get("start_time", None)
        end_time_param = request.query_params.get("end_time", None)

        id = request.query_params.get("id")
        # 根据参数是否存在添加不同筛选条件
        if id is not None:
            queryset = queryset.filter(id=id)
        if (start_time_param and end_time_param) is not None:
            start_time_param_trans = datetime.strptime(start_time_param, "%Y-%m-%d %H:%M:%S")
            end_time_param_trans = datetime.strptime(end_time_param, "%Y-%m-%d %H:%M:%S")
            print(start_time_param_trans)
            print(end_time_param_trans)
            queryset = queryset.filter(timestamp__gte=start_time_param_trans, timestamp__lte=end_time_param_trans)
        else:
            print("aaaaa")

        return queryset


class Ck_M1_hourly_productionView(APIView):
    # 设置默认的每页数据量
    page_size = 10
    # 设置在API DOC的字段显示
    schema = AutoSchema(
        manual_fields=[
            Field(name="id", required=False, location="query", schema=coreschema.Number(description="id")),
            Field(name="page", required=False, location="query", schema=coreschema.String(description="第几页")),
            Field(
                name="page_size",
                required=False,
                location="query",
                schema=coreschema.String(description="页面大小"),
            ),
            Field(
                name="start_time",
                required=False,
                location="query",
                schema=coreschema.String(description="开始日期时间，格式示例：2024-07-01 16:34:42"),
            ),
            Field(
                name="end_time",
                required=False,
                location="query",
                schema=coreschema.String(description="结束日期时间，格式示例：2024-07-10 16:34:42"),
            ),
        ]
    )

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = Ck_M1_hourly_productionSerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def get_queryset(self, request, *args, **kwargs):
        # 这里实现获取数据集合的逻辑
        # 例如: return Model.objects.all()
        # 创建一个基础的查询集
        # queryset = OutputLoopa.objects.using("as_manage_analysis").annotate(staion_order=F('staion')).order_by('staion_order')
        queryset = GdCk01.objects.using("sql_server_dtrdb").filter().order_by("timestamp")
        # 获取请求的参数

        start_time_param = request.query_params.get("start_time", None)
        end_time_param = request.query_params.get("end_time", None)

        id = request.query_params.get("id")
        # 根据参数是否存在添加不同筛选条件
        if id is not None:
            queryset = queryset.filter(id=id)
        if (start_time_param and end_time_param) is not None:
            start_time_param_trans = datetime.strptime(start_time_param, "%Y-%m-%d %H:%M:%S")
            end_time_param_trans = datetime.strptime(end_time_param, "%Y-%m-%d %H:%M:%S")
            print(start_time_param_trans)
            print(end_time_param_trans)
            queryset = queryset.filter(timestamp__gte=start_time_param_trans, timestamp__lte=end_time_param_trans)
        else:
            print("aaaaa")

        return queryset


class HD_M1_hourly_productionView(APIView):
    # 设置默认的每页数据量
    page_size = 10
    # 设置在API DOC的字段显示
    schema = AutoSchema(
        manual_fields=[
            Field(name="id", required=False, location="query", schema=coreschema.Number(description="id")),
            Field(name="page", required=False, location="query", schema=coreschema.String(description="第几页")),
            Field(
                name="page_size",
                required=False,
                location="query",
                schema=coreschema.String(description="页面大小"),
            ),
            Field(
                name="start_time",
                required=False,
                location="query",
                schema=coreschema.String(description="开始日期时间，格式示例：2024-07-01 16:34:42"),
            ),
            Field(
                name="end_time",
                required=False,
                location="query",
                schema=coreschema.String(description="结束日期时间，格式示例：2024-07-10 16:34:42"),
            ),
        ]
    )

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = HD_M1_hourly_productionSerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def get_queryset(self, request, *args, **kwargs):
        # 这里实现获取数据集合的逻辑
        # 例如: return Model.objects.all()
        # 创建一个基础的查询集
        # queryset = OutputLoopa.objects.using("as_manage_analysis").annotate(staion_order=F('staion')).order_by('staion_order')
        queryset = GdHd01.objects.using("sql_server_dtrdb").filter().order_by("timestamp")
        # 获取请求的参数

        start_time_param = request.query_params.get("start_time", None)
        end_time_param = request.query_params.get("end_time", None)

        id = request.query_params.get("id")
        # 根据参数是否存在添加不同筛选条件
        if id is not None:
            queryset = queryset.filter(id=id)
        if (start_time_param and end_time_param) is not None:
            start_time_param_trans = datetime.strptime(start_time_param, "%Y-%m-%d %H:%M:%S")
            end_time_param_trans = datetime.strptime(end_time_param, "%Y-%m-%d %H:%M:%S")
            print(start_time_param_trans)
            print(end_time_param_trans)
            queryset = queryset.filter(timestamp__gte=start_time_param_trans, timestamp__lte=end_time_param_trans)
        else:
            print("aaaaa")

        return queryset


class HD_M2_hourly_productionView(APIView):
    # 设置默认的每页数据量
    page_size = 10
    # 设置在API DOC的字段显示
    schema = AutoSchema(
        manual_fields=[
            Field(name="id", required=False, location="query", schema=coreschema.Number(description="id")),
            Field(name="page", required=False, location="query", schema=coreschema.String(description="第几页")),
            Field(
                name="page_size",
                required=False,
                location="query",
                schema=coreschema.String(description="页面大小"),
            ),
            Field(
                name="start_time",
                required=False,
                location="query",
                schema=coreschema.String(description="开始日期时间，格式示例：2024-07-01 16:34:42"),
            ),
            Field(
                name="end_time",
                required=False,
                location="query",
                schema=coreschema.String(description="结束日期时间，格式示例：2024-07-10 16:34:42"),
            ),
        ]
    )

    def get(self, request, *args, **kwargs):
        # 获取数据列表
        data_list = self.get_queryset(request)

        # 获取页码
        page = request.GET.get("page")

        # 根据URL参数可能提供的每页数据量来覆盖默认设置
        try:
            page_size = int(request.GET.get("page_size", self.page_size))
            print(page_size)
        except ValueError:
            page_size = self.page_size
            print(page_size)

        # 创建Paginator对象
        paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

        try:
            # 获取指定页的数据
            queryset_page = paginator.page(page)
        except PageNotAnInteger:
            # 如果页码不是一个整数，展示第一页
            queryset_page = paginator.page(1)
        except EmptyPage:
            # 如果页码超出范围(例如9999)，展示最后一页
            queryset_page = paginator.page(paginator.num_pages)

        # 序列化数据
        s = HD_M2_hourly_productionSerializer(instance=queryset_page, many=True)
        return Response(s.data, status=status.HTTP_200_OK)

    def get_queryset(self, request, *args, **kwargs):
        # 这里实现获取数据集合的逻辑
        # 例如: return Model.objects.all()
        # 创建一个基础的查询集
        # queryset = OutputLoopa.objects.using("as_manage_analysis").annotate(staion_order=F('staion')).order_by('staion_order')
        queryset = GdHd02.objects.using("sql_server_dtrdb").filter().order_by("timestamp")
        # 获取请求的参数

        start_time_param = request.query_params.get("start_time", None)
        end_time_param = request.query_params.get("end_time", None)

        id = request.query_params.get("id")
        # 根据参数是否存在添加不同筛选条件
        if id is not None:
            queryset = queryset.filter(id=id)
        if (start_time_param and end_time_param) is not None:
            start_time_param_trans = datetime.strptime(start_time_param, "%Y-%m-%d %H:%M:%S")
            end_time_param_trans = datetime.strptime(end_time_param, "%Y-%m-%d %H:%M:%S")
            print(start_time_param_trans)
            print(end_time_param_trans)
            queryset = queryset.filter(timestamp__gte=start_time_param_trans, timestamp__lte=end_time_param_trans)
        else:
            print("aaaaa")

        return queryset


# class LoopA_hourly_productionView(APIView):
#     pass

# class MyPagination(PageNumberPagination):
#     page_size = 20  # 设置默认分页长度为10
#     page_size_query_param = "page_size"  # 允许通过查询参数改变分页长度
#     max_page_size = 100  # 最大分页长度

#     project_param = "project"
#     project_query_description = _("项目")
#     page_query_param = "page"
#     page_query_description = _("要查询哪一页？")
#     page_size_query_description = _("每页要查询多少条数据？")

#     def get_schema_fields(self, view):
#         assert coreapi is not None, "coreapi must be installed to use `get_schema_fields()`"
#         if coreapi is not None:
#             warnings.warn(
#                 "CoreAPI compatibility is deprecated and will be removed in DRF 3.17", RemovedInDRF317Warning
#             )
#         assert coreschema is not None, "coreschema must be installed to use `get_schema_fields()`"
#         fields = [
#             Field(
#                 name=self.page_query_param,
#                 required=False,
#                 location="query",
#                 schema=coreschema.Integer(title="Page", description=force_str(self.page_query_description)),
#             ),
#         ]
#         if self.page_size_query_param is not None:
#             fields.append(
#                 Field(
#                     name=self.page_size_query_param,
#                     required=False,
#                     location="query",
#                     schema=coreschema.Integer(
#                         title="Page size", description=force_str(self.page_size_query_description)
#                     ),
#                 )
#             )
#         return fields


# # Create your views here.


# class AndonPullCallsView(APIView):
#     # 设置默认的每页数据量
#     page_size = 10
#     # 设置在API DOC的字段显示
#     schema = AutoSchema(
#         manual_fields=[
#             Field(name="id", required=False, location="query", schema=coreschema.Number(description="id")),
#             Field(
#                 name="point_id",
#                 required=False,
#                 location="query",
#                 schema=coreschema.String(description="point_id"),
#             ),
#             Field(
#                 name="sub_area",
#                 required=False,
#                 location="query",
#                 schema=coreschema.String(description="区域"),
#             ),
#             Field(
#                 name="equipment_id",
#                 required=False,
#                 location="query",
#                 schema=coreschema.String(description="工位"),
#             ),
#             Field(
#                 name="page", required=False, location="query", schema=coreschema.String(description="第几页")
#             ),
#             Field(
#                 name="page_size",
#                 required=False,
#                 location="query",
#                 schema=coreschema.String(description="页面大小"),
#             ),
#             Field(
#                 name="start_time",
#                 required=False,
#                 location="query",
#                 schema=coreschema.String(description="开始日期时间，格式示例：2024-07-01 16:34:42"),
#             ),
#             Field(
#                 name="end_time",
#                 required=False,
#                 location="query",
#                 schema=coreschema.String(description="结束日期时间，格式示例：2024-07-10 16:34:42"),
#             ),
#         ]
#     )

#     def get(self, request, *args, **kwargs):
#         # 获取数据列表
#         data_list = self.get_queryset(request)

#         # 获取页码
#         page = request.GET.get("page")

#         # 根据URL参数可能提供的每页数据量来覆盖默认设置
#         try:
#             page_size = int(request.GET.get("page_size", self.page_size))
#             print(page_size)
#         except ValueError:
#             page_size = self.page_size
#             print(page_size)

#         # 创建Paginator对象
#         paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

#         try:
#             # 获取指定页的数据
#             queryset_page = paginator.page(page)
#         except PageNotAnInteger:
#             # 如果页码不是一个整数，展示第一页
#             queryset_page = paginator.page(1)
#         except EmptyPage:
#             # 如果页码超出范围(例如9999)，展示最后一页
#             queryset_page = paginator.page(paginator.num_pages)

#         # 序列化数据
#         s = AndonPullCallsSerializer(instance=queryset_page, many=True)
#         return Response(s.data, status=status.HTTP_200_OK)

#     def get_queryset(self, request, *args, **kwargs):
#         # 这里实现获取数据集合的逻辑
#         # 例如: return Model.objects.all()
#         # 创建一个基础的查询集
#         # queryset = OutputLoopa.objects.using("as_manage_analysis").annotate(staion_order=F('staion')).order_by('staion_order')
#         queryset = AndonPullCalls.objects.using("default").filter().order_by("start_time")
#         # 获取请求的参数

#         start_time_param = request.query_params.get("start_time", None)
#         end_time_param = request.query_params.get("end_time", None)

#         id = request.query_params.get("id")
#         sub_area = request.query_params.get("sub_area")
#         equipment_id = request.query_params.get("equipment_id")
#         # 根据参数是否存在添加不同筛选条件
#         if id is not None:
#             queryset = AndonPullCalls.objects.using("default").filter(id=id)
#         if sub_area is not None:
#             queryset = queryset.filter(sub_area__contains=sub_area)
#         if equipment_id is not None:
#             queryset = queryset.filter(equipment_id__contains=equipment_id)
#         if (start_time_param and end_time_param) is not None:
#             start_time_param_trans = datetime.strptime(start_time_param, "%Y-%m-%d %H:%M:%S")
#             end_time_param_trans = datetime.strptime(end_time_param, "%Y-%m-%d %H:%M:%S")
#             print(start_time_param_trans)
#             print(end_time_param_trans)
#             queryset = queryset.filter(
#                 start_time__gte=start_time_param_trans, start_time__lte=end_time_param_trans
#             )
#         else:
#             print("aaaaa")

#         return queryset


# class EquipmentViewSet(viewsets.ModelViewSet):
#     queryset = Equipment.objects.all()
#     serializer_class = EquipmentSerializer
#     # 这是视图级别配置分页
#     pagination_class = MyPagination

#     @action(detail=False, methods=["get"])
#     def custom_list(self, request):
#         queryset = self.get_queryset()
#         page = self.paginate_queryset(queryset)
#         if page is not None:
#             serializer = self.get_serializer(page, many=True)
#             return self.get_paginated_response(serializer.data)

#         serializer = self.get_serializer(queryset, many=True)
#         return Response(serializer.data)


# # 节拍过滤器
# class LastCtFilter(FilterSet):
#     class Meta:
#         model = LastCtStatistics
#         fields = ["sub_area", "equipment_id", "point_id"]


# class LastCtStatisticsViewSet(viewsets.ModelViewSet):
#     queryset = LastCtStatistics.objects.all()

#     serializer_class = LastCtStatisticsSerializer
#     # 这是视图级别配置分页
#     pagination_class = MyPagination

#     filter_backends = [DjangoFilterBackend]
#     filterset_class = LastCtFilter

#     def get_queryset(self):
#         queryset = super().get_queryset()
#         return queryset

#     @action(detail=False, methods=["get"])
#     def custom_list(self, request):
#         queryset = self.get_queryset()
#         page = self.paginate_queryset(queryset)
#         if page is not None:
#             serializer = self.get_serializer(page, many=True)
#             return self.get_paginated_response(serializer.data)

#         serializer = self.get_serializer(queryset, many=True)
#         return Response(serializer.data)


# # 自定义分页
# class LastCtStatisticsView2(APIView):
#     def get(self, request, *args, **kwargs):
#         queryset = LastCtStatistics.objects.all()
#         paginator = MyPagination()
#         page = paginator.paginate_queryset(queryset, request)
#         serializer_class = LastCtStatisticsSerializer(page, many=True)
#         return Response(serializer_class.data)


# class production_totalView(APIView):
#     # 设置默认的每页数据量
#     page_size = 10
#     # 设置在API DOC的字段显示
#     schema = AutoSchema(
#         manual_fields=[
#             Field(name="id", required=False, location="query", schema=coreschema.Number(description="id")),
#             Field(
#                 name="point_id",
#                 required=False,
#                 location="query",
#                 schema=coreschema.String(description="point_id"),
#             ),
#             Field(
#                 name="shift",
#                 required=False,
#                 location="query",
#                 schema=coreschema.Number(description="shift"),
#             ),
#             Field(
#                 name="page", required=False, location="query", schema=coreschema.String(description="第几页")
#             ),
#             Field(
#                 name="page_size",
#                 required=False,
#                 location="query",
#                 schema=coreschema.String(description="页面大小"),
#             ),
#             Field(
#                 name="start_time",
#                 required=False,
#                 location="query",
#                 schema=coreschema.String(description="开始日期时间，格式示例：2024-07-01"),
#             ),
#             Field(
#                 name="end_time",
#                 required=False,
#                 location="query",
#                 schema=coreschema.String(description="结束日期时间，格式示例：2024-07-10"),
#             ),
#         ]
#     )

#     def get(self, request, *args, **kwargs):
#         # 获取数据列表
#         data_list = self.get_queryset(request)

#         # 获取页码
#         page = request.GET.get("page")

#         # 根据URL参数可能提供的每页数据量来覆盖默认设置
#         try:
#             page_size = int(request.GET.get("page_size", self.page_size))
#             print(page_size)
#         except ValueError:
#             page_size = self.page_size
#             print(page_size)

#         # 创建Paginator对象
#         paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

#         try:
#             # 获取指定页的数据
#             queryset_page = paginator.page(page)
#         except PageNotAnInteger:
#             # 如果页码不是一个整数，展示第一页
#             queryset_page = paginator.page(1)
#         except EmptyPage:
#             # 如果页码超出范围(例如9999)，展示最后一页
#             queryset_page = paginator.page(paginator.num_pages)

#         # 序列化数据
#         s = production_totalSerializer(instance=queryset_page, many=True)
#         return Response(s.data, status=status.HTTP_200_OK)

#     def get_queryset(self, request, *args, **kwargs):
#         # 这里实现获取数据集合的逻辑
#         # 例如: return Model.objects.all()
#         # 创建一个基础的查询集
#         # queryset = OutputLoopa.objects.using("as_manage_analysis").annotate(staion_order=F('staion')).order_by('staion_order')
#         queryset = ProductionTotal.objects.using("default").filter().order_by("id")
#         # 获取请求的参数

#         start_time_param = request.query_params.get("start_time", None)
#         end_time_param = request.query_params.get("end_time", None)

#         id = request.query_params.get("id")
#         point_id = request.query_params.get("point_id")
#         shift = request.query_params.get("shift")
#         # 根据参数是否存在添加不同筛选条件
#         if id is not None:
#             queryset = queryset.filter(id=id)
#         if point_id is not None:
#             queryset = queryset.filter(point_id__contains=point_id)
#         if shift is not None:
#             queryset = queryset.filter(shift__contains=shift)
#         if (start_time_param and end_time_param) is not None:
#             start_time_param_trans = datetime.strptime(start_time_param, "%Y-%m-%d")
#             end_time_param_trans = datetime.strptime(end_time_param, "%Y-%m-%d")
#             print(start_time_param_trans)
#             print(end_time_param_trans)
#             queryset = queryset.filter(
#                 production_date__gte=start_time_param_trans, production_date__lte=end_time_param_trans
#             )
#         else:
#             print("aaaaa")

#         return queryset


# class Top20ShutdownView(APIView):
#     # 设置默认的每页数据量
#     page_size = 10
#     # 设置在API DOC的字段显示
#     schema = AutoSchema(
#         manual_fields=[
#             Field(name="id", required=False, location="query", schema=coreschema.Number(description="id")),
#             Field(
#                 name="point_id",
#                 required=False,
#                 location="query",
#                 schema=coreschema.String(description="point_id"),
#             ),
#             Field(
#                 name="equipment_id",
#                 required=False,
#                 location="query",
#                 schema=coreschema.String(description="equipment_id"),
#             ),
#             Field(
#                 name="sub_area",
#                 required=False,
#                 location="query",
#                 schema=coreschema.String(description="sub_area"),
#             ),
#             Field(
#                 name="shift",
#                 required=False,
#                 location="query",
#                 schema=coreschema.Number(description="shift"),
#             ),
#             Field(
#                 name="call_type",
#                 required=False,
#                 location="query",
#                 schema=coreschema.String(description="call_type"),
#             ),
#             Field(
#                 name="page", required=False, location="query", schema=coreschema.String(description="第几页")
#             ),
#             Field(
#                 name="page_size",
#                 required=False,
#                 location="query",
#                 schema=coreschema.String(description="页面大小"),
#             ),
#             Field(
#                 name="start_time",
#                 required=False,
#                 location="query",
#                 schema=coreschema.String(description="开始日期时间，格式示例：2024-07-01 16:34:42"),
#             ),
#             Field(
#                 name="end_time",
#                 required=False,
#                 location="query",
#                 schema=coreschema.String(description="结束日期时间，格式示例：2024-07-10 16:34:42"),
#             ),
#         ]
#     )

#     def get(self, request, *args, **kwargs):
#         # 获取数据列表
#         data_list = self.get_queryset(request)

#         # 获取页码
#         page = request.GET.get("page")

#         # 根据URL参数可能提供的每页数据量来覆盖默认设置
#         try:
#             page_size = int(request.GET.get("page_size", self.page_size))
#             print(page_size)
#         except ValueError:
#             page_size = self.page_size
#             print(page_size)

#         # 创建Paginator对象
#         paginator = Paginator(data_list, page_size)  # page_size是每页显示的数量

#         try:
#             # 获取指定页的数据
#             queryset_page = paginator.page(page)
#         except PageNotAnInteger:
#             # 如果页码不是一个整数，展示第一页
#             queryset_page = paginator.page(1)
#         except EmptyPage:
#             # 如果页码超出范围(例如9999)，展示最后一页
#             queryset_page = paginator.page(paginator.num_pages)

#         # 序列化数据
#         s = Top20ShutdownSerializer(instance=queryset_page, many=True)
#         return Response(s.data, status=status.HTTP_200_OK)

#     def get_queryset(self, request, *args, **kwargs):
#         # 这里实现获取数据集合的逻辑
#         # 例如: return Model.objects.all()
#         # 创建一个基础的查询集
#         # queryset = OutputLoopa.objects.using("as_manage_analysis").annotate(staion_order=F('staion')).order_by('staion_order')
#         queryset = Top20Shutdown.objects.using("default").filter().order_by("id")
#         # 获取请求的参数

#         start_time_param = request.query_params.get("start_time", None)
#         end_time_param = request.query_params.get("end_time", None)

#         id = request.query_params.get("id")
#         point_id = request.query_params.get("point_id")
#         equipment_id = request.query_params.get("equipment_id")
#         sub_area = request.query_params.get("sub_area")
#         shift = request.query_params.get("shift")
#         call_type = request.query_params.get("call_type")
#         # 根据参数是否存在添加不同筛选条件
#         if id is not None:
#             queryset = queryset.filter(id=id)
#         if point_id is not None:
#             queryset = queryset.filter(point_id__contains=point_id)
#         if shift is not None:
#             queryset = queryset.filter(shift__contains=shift)
#         if equipment_id is not None:
#             queryset = queryset.filter(equipment_id__contains=equipment_id)
#         if sub_area is not None:
#             queryset = queryset.filter(sub_area__contains=sub_area)
#         if call_type is not None:
#             queryset = queryset.filter(call_type__contains=call_type)
#         if (start_time_param and end_time_param) is not None:
#             start_time_param_trans = datetime.strptime(start_time_param, "%Y-%m-%d %H:%M:%S")
#             end_time_param_trans = datetime.strptime(end_time_param, "%Y-%m-%d %H:%M:%S")
#             print(start_time_param_trans)
#             print(end_time_param_trans)
#             queryset = queryset.filter(
#                 start_time__gte=start_time_param_trans, start_time__lte=end_time_param_trans
#             )
#         else:
#             print("aaaaa")

#         return queryset


# class Top20ShutdownViewSet(viewsets.ModelViewSet):
#     queryset = Top20Shutdown.objects.all()
#     serializer_class = Top20ShutdownSerializer
#     # 这是视图级别配置分页
#     pagination_class = MyPagination

#     @action(detail=False, methods=["get"])
#     def custom_list(self, request):
#         queryset = self.get_queryset()
#         page = self.paginate_queryset(queryset)
#         if page is not None:
#             serializer = self.get_serializer(page, many=True)
#             return self.get_paginated_response(serializer.data)

#         serializer = self.get_serializer(queryset, many=True)
#         return Response(serializer.data)


# class MachineFaultHistoryViewSet(viewsets.ModelViewSet):
#     queryset = MachineFaultHistory.objects.all()
#     serializer_class = MachineFaultHistorySerializer
#     # 这是视图级别配置分页
#     pagination_class = MyPagination

#     @action(detail=False, methods=["get"])
#     def custom_list(self, request):
#         queryset = self.get_queryset()
#         page = self.paginate_queryset(queryset)
#         if page is not None:
#             serializer = self.get_serializer(page, many=True)
#             return self.get_paginated_response(serializer.data)

#         serializer = self.get_serializer(queryset, many=True)
#         return Response(serializer.data)


# class ProductionHourlyViewSet(viewsets.ModelViewSet):
#     queryset = ProductionHourly.objects.all()
#     serializer_class = ProductionHourlySerializer
#     # 这是视图级别配置分页
#     pagination_class = MyPagination

#     @action(detail=False, methods=["get"])
#     def custom_list(self, request):
#         queryset = self.get_queryset()
#         page = self.paginate_queryset(queryset)
#         if page is not None:
#             serializer = self.get_serializer(page, many=True)
#             return self.get_paginated_response(serializer.data)

#         serializer = self.get_serializer(queryset, many=True)
#         return Response(serializer.data)


# class ScheduleViewSet(viewsets.ModelViewSet):
#     queryset = Schedule.objects.all()
#     serializer_class = ScheduleSerializer
#     # 这是视图级别配置分页
#     pagination_class = MyPagination

#     @action(detail=False, methods=["get"])
#     def custom_list(self, request):
#         queryset = self.get_queryset()
#         page = self.paginate_queryset(queryset)
#         if page is not None:
#             serializer = self.get_serializer(page, many=True)
#             return self.get_paginated_response(serializer.data)

#         serializer = self.get_serializer(queryset, many=True)
#         return Response(serializer.data)


# # 刀具记录过滤器
# class ToolChangeLogFilter(FilterSet):
#     class Meta:
#         model = ToolChangeLog
#         project = CharFilter(field_name="project", help_text="过滤标题")
#         fields = ["project", "sub_area", "equipment"]


# class ToolChangeLogViewSet(viewsets.ModelViewSet, FilterSet):
#     queryset = ToolChangeLog.objects.all()
#     serializer_class = ToolChangeLogSerializer
#     # 这是视图级别配置分页
#     pagination_class = MyPagination

#     filter_backends = [DjangoFilterBackend]
#     filterset_class = ToolChangeLogFilter

#     # 根据前端传递的参数自定义查询集
#     def get_queryset(self):
#         queryset = super().get_queryset()
#         # query_params = self.request.query_params
#         # equipment_param = query_params.get("equipment", None)

#         # if equipment_param:
#         #     queryset = queryset.filter(equipment=equipment_param)
#         #     print("这是" + str(equipment_param))

#         return queryset

#     @action(detail=False, methods=["get"])
#     def custom_list(self, request):
#         queryset = self.get_queryset()

#         page = self.paginate_queryset(queryset)
#         if page is not None:
#             serializer = self.get_serializer(page, many=True)
#             return self.get_paginated_response(serializer.data)

#         serializer = self.get_serializer(queryset, many=True)
#         return Response(serializer.data)


# # 测试用
# class test(APIView):
#     authentication_classes = []

#     data_frombackend1 = {
#         "op2060": [
#             {"name": "设备停机", "start_time": "2023-03-01 09:00:00", "end_time": "2023-03-01 09:10:00"},
#             {"name": "满位", "start_time": "2023-03-01 09:10:00", "end_time": "2023-03-01 09:13:00"},
#             {"name": "有托盘", "start_time": "2023-03-01 09:13:00", "end_time": "2023-03-01 09:15:00"},
#             {"name": "无托盘", "start_time": "2023-03-01 09:15:00", "end_time": "2023-03-01 09:27:00"},
#             {"name": "设备故障", "start_time": "2023-03-01 09:27:00", "end_time": "2023-03-01 09:31:00"},
#             {"name": "空位", "start_time": "2023-03-01 09:31:00", "end_time": "2023-03-01 09:33:00"},
#         ],
#         "op2070": [
#             {"name": "有托盘", "start_time": "2023-03-01 09:00:00", "end_time": "2023-03-01 09:13:10"},
#             {"name": "满位", "start_time": "2023-03-01 09:13:10", "end_time": "2023-03-01 09:15:10"},
#             {"name": "有托盘", "start_time": "2023-03-01 09:15:00", "end_time": "2023-03-01 09:19:00"},
#             {"name": "无托盘", "start_time": "2023-03-01 09:19:00", "end_time": "2023-03-01 09:29:00"},
#             {"name": "设备故障", "start_time": "2023-03-01 09:29:00", "end_time": "2023-03-01 09:39:00"},
#             {"name": "空位", "start_time": "2023-03-01 09:39:00", "end_time": "2023-03-01 09:43:00"},
#         ],
#         "op2080": [
#             {"name": "有托盘", "start_time": "2023-03-01 09:00:00", "end_time": "2023-03-01 09:05:10"},
#             {"name": "满位", "start_time": "2023-03-01 09:05:10", "end_time": "2023-03-01 09:10:10"},
#             {"name": "有托盘", "start_time": "2023-03-01 09:10:10", "end_time": "2023-03-01 09:11:00"},
#             {"name": "无托盘", "start_time": "2023-03-01 09:11:00", "end_time": "2023-03-01 09:47:00"},
#             {"name": "设备故障", "start_time": "2023-03-01 09:47:00", "end_time": "2023-03-01 09:49:00"},
#             {"name": "空位", "start_time": "2023-03-01 09:49:00", "end_time": "2023-03-01 09:53:00"},
#         ],
#         "op2090": [
#             {"name": "有托盘", "start_time": "2023-03-01 09:00:00", "end_time": "2023-03-01 09:03:10"},
#             {"name": "满位", "start_time": "2023-03-01 09:03:10", "end_time": "2023-03-01 09:05:10"},
#             {"name": "有托盘", "start_time": "2023-03-01 09:05:00", "end_time": "2023-03-01 09:19:00"},
#             {"name": "无托盘", "start_time": "2023-03-01 09:19:00", "end_time": "2023-03-01 09:21:00"},
#             {"name": "设备故障", "start_time": "2023-03-01 09:21:00", "end_time": "2023-03-01 09:31:00"},
#             {"name": "空位", "start_time": "2023-03-01 09:31:00", "end_time": "2023-03-01 09:43:00"},
#         ],
#         "长缸体OP2130-2": [
#             {"name": "有托盘", "start_time": "2023-03-01 09:00:00", "end_time": "2023-03-01 09:13:10"},
#             {"name": "满位", "start_time": "2023-03-01 09:13:10", "end_time": "2023-03-01 09:15:10"},
#             {"name": "有托盘", "start_time": "2023-03-01 09:15:00", "end_time": "2023-03-01 09:19:00"},
#             {"name": "无托盘", "start_time": "2023-03-01 09:19:00", "end_time": "2023-03-01 09:27:00"},
#             {"name": "设备故障", "start_time": "2023-03-01 09:27:00", "end_time": "2023-03-01 09:39:00"},
#             {"name": "空位", "start_time": "2023-03-01 09:39:00", "end_time": "2023-03-01 09:43:00"},
#         ],
#         "冷试OP2210": [
#             {"name": "有托盘", "start_time": "2023-03-01 09:00:00", "end_time": "2023-03-01 09:13:10"},
#             {"name": "满位", "start_time": "2023-03-01 09:13:10", "end_time": "2023-03-01 09:15:10"},
#             {"name": "有托盘", "start_time": "2023-03-01 09:15:00", "end_time": "2023-03-01 09:19:00"},
#             {"name": "无托盘", "start_time": "2023-03-01 09:19:00", "end_time": "2023-03-01 09:27:00"},
#             {"name": "设备故障", "start_time": "2023-03-01 09:27:00", "end_time": "2023-03-01 09:39:00"},
#             {"name": "空位", "start_time": "2023-03-01 09:39:00", "end_time": "2023-03-01 09:43:00"},
#         ],
#     }

#     def get(self, request):
#         # quzhou = StationStatus.objects.filter(sub_area="曲轴M1")
#         # print(quzhou)
#         print("hhhhhhh")
#         return Response(self.data_frombackend1)
